const path = require("path");
const fs = require("fs");
const CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
const webpack = require('webpack');

const uglifyJsPlugin = webpack.optimize.UglifyJsPlugin;


const srcDir = path.resolve(process.cwd(), 'public/javascripts');


function resolve (dir) {
  return path.join(__dirname,dir)
}

//多页面入口
function getEntry() {
  const jsPath = path.resolve(srcDir,'page');
  const dirs = fs.readdirSync(jsPath);
  let matchs = [], files = {};
  dirs.forEach(function (item) {
    matchs = item.match(/(.+)\.js$/);
    if (matchs) {
      files[matchs[1]] = path.resolve(srcDir, 'page', item);
    }
  });
  return files;
}


module.exports = {
  devtool: "#source-map",
  entry: getEntry(),
	output: {
		path: resolve('dist/javascripts'),
		filename: "[name].js"
	},
  module: {
    rules: [

      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [srcDir]
      }
      // ,
      // {
      //   test: require.resolve('jquery'),
      //   loader: 'expose?jQuery'
      // }
    ]
  },
  resolve: {
    alias: {
      // jquery: "jquery/src/jquery"
    }
  },
	plugins: [
    // new webpack.ProvidePlugin({
    //   $: 'jquery',
    //   'jQuery': 'jquery'
    // }),
    new CommonsChunkPlugin('vendor')
    // , new uglifyJsPlugin({
    //   compress: {
    //     warnings: false
    //   }
    // }),

	],
};
